package texts;

import moment.Utiles;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class UserInfoPage extends Utiles {
    public static String url = "http://110.41.84.205:9091/index.html";
    public UserInfoPage() {
        super(url);
    }
    public void userInfoPageRight() {
        // 刷新
        driver.navigate().refresh();
        // 进入个人信息修改页面
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();
        assert driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col-3.d-none.d-md-block.border-end > div > div > a")).getText().equals("我的账户");
    }

    public void updateInfoPage() throws InterruptedException {
        // 修改昵称
        // 定位输入框
        WebElement inputElement = driver.findElement(By.cssSelector("#setting_input_nickName"));
        // 模拟键盘点击和输入
        Actions action = new Actions(driver);
        action.sendKeys(inputElement, "修改昵称测试").build().perform();
        driver.findElement(By.cssSelector("#setting_submit_nickName")).click();
        //修改邮箱
        userInfoPageRight();
        driver.findElement(By.cssSelector("#setting_input_email")).clear();
        driver.findElement(By.cssSelector("#setting_input_email")).sendKeys("123456@qqcom");
        driver.findElement(By.cssSelector("#setting_submit_email")).click();

        // 更新手机号
        userInfoPageRight();
        // 下滑窗口
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0,200)");
        Thread.sleep(200);
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).clear();
        driver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys("123456789");
        driver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();

        // 修改个人简介
        userInfoPageRight();
        // 下滑窗口
        js.executeScript("window.scrollTo(0,800)");
        Thread.sleep(300);
        driver.findElement(By.cssSelector("#settings_textarea_remark")).clear();
        driver.findElement(By.cssSelector("#settings_textarea_remark")).sendKeys("修改个人简介测试");
        driver.findElement(By.cssSelector("#settings_submit_remark")).click();
    }

    /**
     * 修改密码
     */
    public void updatePassword(String oldPassword) throws InterruptedException {
        String newPassword = "123456";
        // 打开页面
        userInfoPageRight();

        // 修改个人简介
        userInfoPageRight();
        // 下滑窗口
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0,800)");
        Thread.sleep(200);
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear();
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys(oldPassword);

        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear();
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys(newPassword);
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear();
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys(newPassword);


        driver.findElement(By.cssSelector("#settings_submit_password")).click();
    }
}
